//
//  EventDetailViewController.m
//  Gruphoto
//
//  Created by Thao Nguyen Huy on 1/8/13.
//  Copyright (c) 2013 CNC Software. All rights reserved.
//

#import "EventDetailViewController.h"
#import "GruphotoTabBarController.h"
#import <QuartzCore/QuartzCore.h>
#import "EventPhotoCell.h"
#import "EventJoinCell.h"
#import "PhotoDetailViewController.h"
#import "UserDetailViewController.h"
#import "InviteFriendViewController.h"
#import "CreateViewController.h"
#import "GruphotoCameraViewController.h"
#import "Event.h"
#import "tableViewEventImages.h"
#import "GruphotoAppDelegate.h"
#import <SDWebImage/UIImageView+WebCache.h>

#define CAMERA_TRANSFORM 1.25299

#define kShareAlbum     @"Share Album"
#define kDeletePhoto    @"Delete Photo"
#define kReportAbuse    @"Report Abuse"
#define kInviteFriend   @"Invite Friend"
#define kEditEvent      @"Edit Event"
#define kUnpublish      @"Unpublish Album"
#define kPostAlbum      @"Post Album"

const NSInteger numberOfPhotosPerRow = 3;

@interface EventDetailViewController (PrivateMethods)
- (void)addBackButtonItem;
- (void)addSettingsButtonItem;
- (void)addDoneButtonItem;
- (void)addCancelButtonItem;
@end

@implementation EventDetailViewController

@synthesize eventPassTimeLabel, eventTitleLabel, authorAvatarImageView, authorNameLabel, deviders, detailButtons, cameraView;
@synthesize numberOfLikesLabel, numberOfPeoplesLabel, numberOfPhotosLabel, photoTableView, userTableView, detailMode;
@synthesize eventIndex, isDeleting, shouldShowTabbar, cameraViewController, event;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UIImageView *titleImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"title_gruphoto.png"]];
        self.navigationItem.titleView = titleImageView;
        [titleImageView release];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    if ([self.navigationController.navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)]) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"background_navigation_bar.png"]
                                                      forBarMetrics:UIBarMetricsDefault];
    }
    self.view.backgroundColor = [UIColor colorWithRed:226/255.0 green:231/255.0 blue:237/255.0 alpha:1.0];
    for (UIView *devider in deviders) {
        if (devider.tag == 1) {
             devider.backgroundColor = [UIColor colorWithRed:129/255.0 green:146/255.0 blue:159/255.0 alpha:1.0];       
        }
        else {
            devider.backgroundColor = [UIColor colorWithRed:190/255.0 green:191/255.0 blue:193/255.0 alpha:1.0];
        }
    }
    authorNameLabel.textColor = [UIColor colorWithRed:107/255.0 green:120/255.0 blue:130/255.0 alpha:1.0];
    numberOfPeoplesLabel.textColor = [UIColor colorWithRed:107/255.0 green:120/255.0 blue:130/255.0 alpha:1.0];
    numberOfPhotosLabel.textColor = [UIColor colorWithRed:107/255.0 green:120/255.0 blue:130/255.0 alpha:1.0];
    numberOfLikesLabel.textColor = [UIColor colorWithRed:107/255.0 green:120/255.0 blue:130/255.0 alpha:1.0];
    eventPassTimeLabel.textColor = [UIColor colorWithRed:107/255.0 green:120/255.0 blue:130/255.0 alpha:1.0];
    eventTitleLabel.textColor = [UIColor colorWithRed:71/255.0 green:138/255.0 blue:197/255.0 alpha:1.0];
    detailType = EventDetailTypeAllPhotos;
    
    if (detailMode != EventDetailModeJoin && detailMode != EventDetailModeCreate) {
        CGRect tableFrame = photoTableView.frame;
        tableFrame.size.height = self.view.frame.size.height - tableFrame.origin.y - 44;
        photoTableView.frame = tableFrame;
        userTableView.frame = tableFrame;
        cameraView.hidden = YES;
    }
    
    [self addBackButtonItem];
    
    if (detailMode == EventDetailModeCreate || detailMode == EventDetailModeSetting) {
        [self addSettingsButtonItem];
    }
    
    if (detailMode == EventDetailModeSetting) {        
        if (eventIndex > 0) {
            if (eventIndex % 2 == 0) {
                eventPassTimeLabel.text = @"Unpublished";
            }
            else {
                eventPassTimeLabel.text = @"03.01.2013";
            }
        }
    }
    
    [self showContentsFromEvent:event];
/*
    if (detailMode == EventDetailModeCreate) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Welcome Admin", @"")
                                                            message:NSLocalizedString(@"Congratulations, your event is ready to receive friends, you can do so by sending a request to join or your friend will see the event on map or list", @"")
                                                           delegate:self
                                                  cancelButtonTitle:NSLocalizedString(@"Dismiss", @"")
                                                  otherButtonTitles:NSLocalizedString(@"Send invitation", @""), nil];
        [alertView show];
        [alertView release];
    }
*/
    [self addPhotosTableViewDefault];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    GruphotoTabBarController *tabBarController = (GruphotoTabBarController *)self.tabBarController;
    [tabBarController hidesFakeTabBar:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.eventTitleLabel = nil;
    self.eventPassTimeLabel = nil;
    self.authorAvatarImageView = nil;
    self.authorNameLabel = nil;
    self.numberOfLikesLabel = nil;
    self.numberOfPeoplesLabel = nil;
    self.numberOfPhotosLabel = nil;
    self.photoTableView = nil;
    self.deviders = nil;
    self.detailButtons = nil;
    self.userTableView = nil;
    self.cameraView = nil;
//    tableViewEventImages = nil;
}

- (void)dealloc {
    self.cameraViewController = nil;
    self.event = nil;
    [userTableView release];
    [photoTableView release];
    [eventPassTimeLabel release];
    [eventTitleLabel release];
    [authorAvatarImageView release];
    [authorNameLabel release];
    [numberOfLikesLabel release];
    [numberOfPeoplesLabel release];
    [numberOfPhotosLabel release];
    [deviders release];
    [detailButtons release];
    [cameraView release];
    [tableViewEventImages release];
    [super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Private methods
- (void)addBackButtonItem {
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setBounds:CGRectMake(0, 0, 51, 30)];
    [backButton setBackgroundImage:[UIImage imageNamed:@"button_back.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = backButtonItem;
    [backButtonItem release];
}

- (void)addSettingsButtonItem {
    UIButton *settingsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [settingsButton setBounds:CGRectMake(0, 0, 60, 30)];
    [settingsButton setBackgroundImage:[UIImage imageNamed:@"button_settings.png"] forState:UIControlStateNormal];
    [settingsButton addTarget:self action:@selector(settingsButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *settingsButtonItem = [[UIBarButtonItem alloc] initWithCustomView:settingsButton];
    self.navigationItem.rightBarButtonItem = settingsButtonItem;
    [settingsButtonItem release];
}

- (void)addDoneButtonItem {
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [doneButton setBounds:CGRectMake(0, 0, 59, 30)];
    [doneButton setBackgroundImage:[UIImage imageNamed:@"button_delete.png"] forState:UIControlStateNormal];
    [doneButton addTarget:self action:@selector(settingsButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *doneButtonItem = [[UIBarButtonItem alloc] initWithCustomView:doneButton];
    self.navigationItem.rightBarButtonItem = doneButtonItem;
    [doneButtonItem release];
}

- (void)addCancelButtonItem {
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setBounds:CGRectMake(0, 0, 51, 30)];
    [cancelButton setBackgroundImage:[UIImage imageNamed:@"button_cancel.png"] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
    self.navigationItem.leftBarButtonItem = cancelButtonItem;
    [cancelButtonItem release];
}

#pragma mark - Custom methods
- (void)showContentsFromEvent:(Event *)_event {
    eventTitleLabel.text = _event.eventTitle;
    if (_event.eventPasstime == 0) {
        eventPassTimeLabel.text = @"Just now";
    } else {
        // need to calculate
    }
    authorNameLabel.text = [NSString stringWithFormat:@"%@ %@", _event.creater.firstName, _event.creater.lastName];
    numberOfLikesLabel.text = [NSString stringWithFormat:@"%d", _event.numberOfLikes];
    numberOfPeoplesLabel.text = [NSString stringWithFormat:@"%d", _event.numberOfMembers];
    numberOfPhotosLabel.text = [NSString stringWithFormat:@"%d", _event.numberOfImages];
    NSLog(@"avatar: %@", event.creater.avatarUrl);
    if (event.creater.avatarImage != nil) {
        authorAvatarImageView.image = event.creater.avatarImage;
    }
    else {
        if (event.creater.avatarUrl.length > 0) {
            NSURL *avatarUrl = [NSURL URLWithString:event.creater.avatarUrl];
            [authorAvatarImageView setImageWithURL:avatarUrl
                                 placeholderImage:[UIImage imageNamed:@"no_avatar.png"]
                                          options:SDWebImageProgressiveDownload];
        }
    }
}

#pragma mark - Action methods
- (IBAction)backButtonPressed:(id)sender {
    if (detailMode == EventDetailModeCreate || detailMode == EventDetailModeJoin) {
        [self.navigationController dismissModalViewControllerAnimated:YES];
        return;
    }

    GruphotoTabBarController *tabBarController = (GruphotoTabBarController *)self.tabBarController;
    [UIView animateWithDuration:0.2 animations:^{
        if (detailMode != EventDetailModeUserDetail) {
            [tabBarController showFakeTabBar:YES];            
        }
    } completion:^(BOOL finished) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (IBAction)detailButtonPressed:(id)sender {
    detailType = [sender tag];
    for (UIButton *button in detailButtons) {
        if (button.tag == detailType) {
            button.enabled = NO;
        }
        else {
            button.enabled = YES;            
        }
    }
    switch (detailType) {
        case EventDetailTypeAllPhotos:
            [tableViewEventImages displayByNewest];
            userTableView.hidden = YES;
            tableViewEventImages.hidden = NO;
            break;
        case EventDetailTypeFavorites:
            [tableViewEventImages displayByMostFavorite];
            userTableView.hidden = YES;
            tableViewEventImages.hidden = NO;
//            [photoTableView reloadData];
            break;
        case EventDetailTypeUsers:
            userTableView.hidden = NO;
            tableViewEventImages.hidden = YES;
            break;
        default:
            break;
    }
}

- (IBAction)settingsButtonPressed:(id)sender {
    if (isDeleting) {
        isDeleting = NO;
        [self addSettingsButtonItem];
        [photoTableView reloadData];
        [self addBackButtonItem];
    }
    else {
        UIActionSheet *actionSheet = [[UIActionSheet alloc] init];
        actionSheet.delegate = self;
        if (eventIndex > 0) {
            if (eventIndex % 2 == 0) {
                [actionSheet addButtonWithTitle:kPostAlbum];
            }
            else {
                [actionSheet addButtonWithTitle:kUnpublish];
            }
        }
        
        [actionSheet addButtonWithTitle:kShareAlbum];
        [actionSheet addButtonWithTitle:kDeletePhoto];
        if (eventIndex == 0) {
            [actionSheet addButtonWithTitle:kReportAbuse];
            [actionSheet addButtonWithTitle:kInviteFriend];
        }
        [actionSheet addButtonWithTitle:kEditEvent];
        actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:@"Cancel"];
        actionSheet.tag = ActionSheetTagSettings;
        [actionSheet showInView:self.view];
        [actionSheet release];
    }
}

- (IBAction)cancelButtonPressed:(id)sender {
    isDeleting = NO;
    [self addSettingsButtonItem];
    [photoTableView reloadData];
    
    [self addBackButtonItem];
}

- (IBAction)takePictureButtonPressed:(id)sender {
    /*
    //truong hoang: upload image no use camera
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:[GruphotoAppDelegate sharedDelegate].loggedInUser.token forKey:kUserToken];
    [params setObject:[NSString stringWithFormat:@"128"] forKey:kEventId];
    NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"large_image.png"]);
    [params setObject:imageData forKey:kPhoto];
    
    [TNHRequestHelper sendPostRequest:kUploadPhotoToEventUrl
                              withTag:RequestTagUploadPhoto
                               params:params
                             delegate:nil];
    */

    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Warning message", @"")
                                                            message:NSLocalizedString(@"Camera is not available", @"")
                                                           delegate:nil
                                                  cancelButtonTitle:NSLocalizedString(@"OK", @"")
                                                  otherButtonTitles:nil];
        [alertView show];
        [alertView release];
        return;
    }
    else {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.allowsEditing = NO;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        // Hide the controls:
        imagePicker.showsCameraControls = NO;
        imagePicker.navigationBarHidden = YES;
        
        // Make camera view full screen:
        imagePicker.wantsFullScreenLayout = YES;
        imagePicker.cameraViewTransform = CGAffineTransformScale(imagePicker.cameraViewTransform, CAMERA_TRANSFORM, CAMERA_TRANSFORM);
        
        self.cameraViewController = nil;
        cameraViewController = [[GruphotoCameraViewController alloc] initWithNibName:@"GruphotoCameraViewController" bundle:nil];
        imagePicker.cameraOverlayView = cameraViewController.view;
        imagePicker.delegate = cameraViewController;
        cameraViewController.imagePicker = imagePicker;
        cameraViewController.eventId = event.eventId;
        NSLog(@"id: %d", event.eventId);
        [self presentModalViewController:imagePicker animated:YES];
        [imagePicker release];        
    }
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (alertView.tag == AlertViewTagWelcomeToCreatedEvent) {
        
    }
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
    NSLog(@"button index: %d", buttonIndex);
    if (actionSheet.tag = ActionSheetTagSettings) {
        if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:kPostAlbum]) {
             
        }
        else
        if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:kUnpublish]) {
            
        }
        else
        if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:kShareAlbum]) {
            UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share this album by"
                                                                     delegate:self
                                                            cancelButtonTitle:@"Cancel"
                                                       destructiveButtonTitle:nil
                                                            otherButtonTitles:@"Flicker", @"Google+", @"Twitter", @"Facebook", nil];
            actionSheet.tag = ActionSheetTagShare;
            [actionSheet showInView:self.view];
            [actionSheet release];
        }
        else
        if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:kDeletePhoto]) {
            isDeleting = YES;
            [self addDoneButtonItem];
            [self detailButtonPressed:[detailButtons objectAtIndex:0]];
            
            [self addCancelButtonItem];
        }
        else
        if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:kReportAbuse]) {
            if ([MFMailComposeViewController canSendMail]) {
                MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
                mailComposer.mailComposeDelegate = self;
                [mailComposer setToRecipients:[NSArray arrayWithObject:@"tamirkeren1@gmail.com"]];
                [mailComposer setSubject:@"Report abuse in event"];
                [self presentModalViewController:mailComposer animated:YES];
                [mailComposer release];
            }
            else {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:kReportAbuse
                                                                    message:@"Email is not set on device"
                                                                   delegate:self
                                                          cancelButtonTitle:@"OK"
                                                          otherButtonTitles: nil];
                [alertView show];
                [alertView release];
            }
        }
        else
        if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:kInviteFriend]) {
            InviteFriendViewController *inviteFriendViewController = [[InviteFriendViewController alloc] initWithNibName:@"InviteFriendViewController" bundle:nil];
            inviteFriendViewController.isPrivate = event.isPrivate;
            inviteFriendViewController.code = event.code;
            [self.navigationController pushViewController:inviteFriendViewController animated:YES];
            [inviteFriendViewController release];
        }
        else
        if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:kEditEvent]) {
            CreateViewController *createViewController = [[CreateViewController alloc] initWithNibName:@"CreateViewController" bundle:nil];
            createViewController.editMode = EventEditModeUpdate;
            [self.navigationController pushViewController:createViewController animated:YES];
            [createViewController release];
        }
        
    }
    else if (actionSheet.tag = ActionSheetTagShare) {
        
    }
    else {
        
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == photoTableView) {
        NSInteger numberOfRows = event.numberOfImages / 3;
        if (event.numberOfImages % 3 > 0) {
            numberOfRows += 1;
        }
        return numberOfRows;
    }
    else {
        return event.numberOfMembers;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *EventPhotoCellId = @"EventPhotoCell";
    static NSString *EventUserCellId = @"EventJoinCell";
    if (tableView == photoTableView) {
        EventPhotoCell *cell = (EventPhotoCell *)[tableView dequeueReusableCellWithIdentifier:EventPhotoCellId];
        if (cell == nil) {
            cell = [EventPhotoCell cellFromNib];
        }
        for (UIView *holder in cell.placeHolders) {
            CGRect frame = CGRectMake(0, 0, kViewWidth, kViewHeight);
            EventPhotoView *view = [EventPhotoView viewFromNib];
//            view.delegate = self;
            view.frame = frame;
            if (detailType == EventDetailTypeAllPhotos) {
                view.footerView.hidden = YES;
            }
            else if (detailType == EventDetailTypeFavorites) {
                if (indexPath.row < 1) {
                    view.footerView.hidden = NO;
                    NSInteger likes = 69 - 7 * holder.tag;
                    view.numberLikesLabel.text = [NSString stringWithFormat:@"%d", likes];
                }
                else {
                    view.footerView.hidden = YES;
                }
            }
            view.tag = indexPath.row;
            [holder addSubview:view];
        }
        return cell;
    }
    else {
        EventJoinCell *cell = (EventJoinCell *)[tableView dequeueReusableCellWithIdentifier:EventUserCellId];
        if (cell == nil) {
            cell = [EventJoinCell cellFromNib];
            cell.distanceLabel.hidden = YES;
            cell.detailLabel.text = [NSString stringWithFormat:@"Join %d hours ago", indexPath.row + 1];
        }
        User *user = [event.users objectAtIndex:indexPath.row];
        cell.eventTitleLabel.text = [NSString stringWithFormat:@"%@ %@", user.firstName, user.lastName];
        if (user.avatarUrl.length > 0) {
            [cell.avatarImageView setImageWithURL:[NSURL URLWithString:user.avatarUrl] placeholderImage:[UIImage imageNamed:@"no_avatar.png"] options:SDWebImageProgressiveDownload];
        }
        cell.numberOfLikesLabel.text = [NSString stringWithFormat:@"%d", user.numberOfLikes];
        cell.numberOfPeoplesLabel.text = [NSString stringWithFormat:@"%d", user.numberOfFollowers];
        cell.numberOfPhotosLabel.text = [NSString stringWithFormat:@"%d", user.numberOfPhotos];
        return cell;
    }
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == userTableView) {
        UserDetailViewController *userDetailViewController = [[UserDetailViewController alloc] initWithNibName:@"UserDetailViewController" bundle:nil];
        [self.navigationController pushViewController:userDetailViewController animated:YES];
        [userDetailViewController release];
    }
}

//#pragma mark - EventPhotoView
//- (void)didSelectPhotoAtIndex:(NSInteger)index {
//    PhotoDetailViewController *photoDetailViewController = [[PhotoDetailViewController alloc] initWithNibName:@"PhotoDetailViewController" bundle:nil];
//    photoDetailViewController.info = [[_eventDetail objectForKey:@"photos"] objectAtIndex:index];
//    [self.navigationController pushViewController:photoDetailViewController animated:YES];
//    [photoDetailViewController release];
//}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    NSLog(@"editing info: %@", editingInfo);
    [picker dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissModalViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark - MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    switch (result) {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail compose cancelled.");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail compose saved.");
            break;
        case MFMailComposeResultSent: {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:kReportAbuse
                                                                message:NSLocalizedString(@"Email was sent", @"")
                                                               delegate:nil
                                                      cancelButtonTitle:NSLocalizedString(@"OK", @"")
                                                      otherButtonTitles:nil];
            [alertView show];
            [alertView release];
            break;
        }
        case MFMailComposeResultFailed: {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:kReportAbuse
                                                                message:NSLocalizedString(@"Failure to send email", @"")
                                                               delegate:nil
                                                      cancelButtonTitle:NSLocalizedString(@"OK", @"")
                                                      otherButtonTitles:nil];
            [alertView show];
            [alertView release];
            break;
        }
        default:
            break;
    }
    [controller dismissModalViewControllerAnimated:YES];
}

-(void)addPhotosTableViewDefault {
    tableViewEventImages = [[TableViewEventImages alloc] initWithData:[_eventDetail objectForKey:@"photos"] atFrame:CGRectMake(0, 77, 320, 246)];
    tableViewEventImages.contentInset = UIEdgeInsetsMake(10, 0, 10, 0);
    tableViewEventImages.backgroundColor = [UIColor clearColor];
    tableViewEventImages.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableViewEventImages];
}

@end
